﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using ASPITALIA.ModelVirtualCasting.Cache;

namespace ASPItalia.ModelVirtualCasting.Cache.Repositories
{
	/// <summary>
	/// Tiene in cache il risultato dell'esecuzione di un metodo.
	/// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
    public class CacheThisAttribute : HandlerAttribute
    {
		/// <summary>
		/// La chiave dell'oggetto in cache.
		/// </summary>
        public string Key { get; set; }
		/// <summary>
		/// Il timeout (in secondi). Se omesso, l'elemento non scade.
		/// </summary>
        public int Timeout { get; set; }

        public CacheThisAttribute(string key, int seconds = int.MaxValue)
        {
            this.Key = key;
            this.Timeout = seconds;
        }

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new CacheHandler(Key, Timeout, CacheFactory.Instance);
        }
    }
}
